class SubmitServlet < HTTPServlet::AbstractServlet
 
 
 # handle hash submission
 # @param req client request
 # @param rep client reply
 # @return HTTP status
 # \version 1.0
 # \date   Oct 2009
 # \author Elie
 
  def do_GET(req, resp)   
   
   #prune old clients
   pruneClient()
   body = ""
   #File upload 
   #   puts req.query['file']
   # puts req.query['list']
   if req.query['file'] != nil or (req.query['list'] != nil and req.query['list'] != "")
   
    #one job by algorithm otherwise it is not managable update the wiki

    #def initialize(id, name, desc, algo="md5", salted=false, start, stop, type)

    #form upload
    if (req.query['list'] != nil and req.query['list'] != "")
      hashes = req.query['list']
    else
    #file upload
      hashes = req.query['file']
    end
   
   #testing variable (painfull  !)
   
   if req.query['jobName'] == nil
     resp.body = "jobName not found"
     resp.status = 400 #bad request
     return
   else
     name = req.query['jobName'] 
   end 
   
   
   if req.query['jobDesc'] == nil
    desc = ""
   else
    desc = req.query['jobDesc']
   end 
   

   if req.query['charSet'] == nil or req.query['charSet'] == ""
    charset = "abcdefghijklmnopqrstuvwxz"
   else
    charset= req.query['charSet']
   end 
   
   
   #optionnal param
   type  = req.query['type'].to_i 
   start = req.query['start'].to_i
   stop  = req.query['stop'].to_i
   
   #job id handling
   id = 0
   $jobNum += 1

   #puts name, desc, type.to_s, start.to_s, stop.to_s, hashes
   #ready to do hashs parsing !
   hashes.split(/\n/).each { |h|
    
    found = false
    if data = /\$([^$]+)\$([^$]*)\$(.+)/.match(h)
       puts "data" + data.to_s
       $jobs.each{ |j|
        #if a job with this name and algo exist just add it 
        if j.name ==  name and j.algo == data[1] and j.jobNum == $jobNum
          j.addHash(data[2], data[3])
          found = true
        end
       }
       #didn't find a job, gotta create one 
       if found == false 
         #(jobNum, id, name, desc, algo="md5", salted=false, start=1, stop=14, type=1)
        salted = data[2] != "" ? true : false
        #body += salted ? "salted is true<br/>" : "salted is not true<br/>"
        key = name + id.to_s
        j[key] = Job.new($jobNum, id, name, desc, data[1], salted, start, stop, type, charset)
        id += 1
        j.addHash(data[2], data[3])
        $jobs.push(j)
      end
    else 
      #FIXME debug warning here
    end
   }
   
   
    body += 'job submited'
    resp.status = 202
    resp.body = body
   
   #provide a form
   else
    
   
    #construct the body
    #get headers
    body = headers("Submit hashs to crack")
    
    body += '
    
              <h1>Nightingale job submission interface</h1>
              
              With this interface you can submit a new job to Nightingale.
              If you need help please see the <a href="/static/help.html">help</a> or visit
              the project website.
              
              <form name="hashSubmit" action="/submit" method="POST" enctype="multipart/form-data">
                <h2>Job configuration</h2>
                <table>
                  <tr>
                     <td>Job name</td>
                     <td><input id="jobName" name="jobName" type="text" value="job-'+ $jobNum.to_s
    body +=     '" /></td>
                  </tr>
                  <tr>
                   <td>Description</td>
                   <td><input id="jobDesc" name="jobDesc" type="text" value="" /> </td>
                  </tr>
                  <tr>
                    <td>Attack type</td>
                    <td> Brute force <input name="type" value=1 type="radio" checked=true> Dictionnary <input name="type" value=2 type="radio"></td> 
                  </tr>
                  <tr>
                    <td>Start char</td>
                    <td><input type="text" size=4 name="start" id="start" value="1">
                  </tr>
                  <tr>
                    <td>Stop char</td>
                    <td><input type="text" size=4 name="stop" id="stop" value="8">
                  </tr>
                  <tr>
                    <td>Charset</td>
                    <td><input type="text" size=100 name="charSet" id="charSet" value="abcdefghijklmnopqrstuvwxz">
                  </tr>
                  <tr>
                    <td></td>
                    <td>0123456789</td>
                  </tr>
                  <tr>
                    <td></td>
                    <td>ABCDEFGHIJKLMNOPQRSTUVWXYZ</td>
                  </tr>
                </table>
                
                <h2>Job content</h2>
                Supply a file with hashs: <input type="file" name="file" id="file"><br/>
                <b>or</b> enter a list of hashs  below:<br/> 
                <textarea id="list" name="list" rows="25" cols="100"></textarea>    
              
                <h2>Submit your job</h2>
                <input id="launch" name="launch" type="submit" value="Launch cracking"/>
              </form>
    '
    #get footer
    body += footer()
    
    #send it
    resp.body = body
    raise HTTPStatus::OK   
    
   end
   

  end
  alias do_POST do_GET    # let's accept POST request 
end